/* Copyright (c) 2025 Beijing Semidrive Technology Corporation
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef REGHELPER_H
#define REGHELPER_H

#ifdef __cplusplus
extern "C" {
#endif

#include "Std_Types.h"
/* Version and Check Begin */
/* Version Info Begin */
#define REGHELPER_H_VENDOR_ID    0x8C
#define REGHELPER_H_AR_RELEASE_MAJOR_VERSION    4
#define REGHELPER_H_AR_RELEASE_MINOR_VERSION    3
#define REGHELPER_H_AR_RELEASE_REVISION_VERSION 1
#define REGHELPER_H_SW_MAJOR_VERSION    1
#define REGHELPER_H_SW_MINOR_VERSION    0
#define REGHELPER_H_SW_PATCH_VERSION    0
/* Version Info End */

/* Version Check Begin */
#if !defined(DISABLE_MCAL_ASR_VER_CHECK)
#if ((REGHELPER_H_AR_RELEASE_MAJOR_VERSION != PLATFORM_TYPES_H_AR_RELEASE_MAJOR_VERSION)\
    || (REGHELPER_H_AR_RELEASE_MINOR_VERSION != PLATFORM_TYPES_H_AR_RELEASE_MINOR_VERSION)\
    || (REGHELPER_H_AR_RELEASE_REVISION_VERSION != PLATFORM_TYPES_H_AR_RELEASE_REVISION_VERSION))
#error "Opps, RegHelper.h and Platform_Types.h AutoSAR version not match!"
#endif
#endif  /* !DISABLE_MCAL_ASR_VER_CHECK */
#if !defined(DISABLE_MCAL_SW_VER_CHECK)
#if ((REGHELPER_H_SW_MAJOR_VERSION != PLATFORM_TYPES_H_SW_MAJOR_VERSION)\
    || (REGHELPER_H_SW_MINOR_VERSION != PLATFORM_TYPES_H_SW_MINOR_VERSION)\
    || (REGHELPER_H_SW_PATCH_VERSION != PLATFORM_TYPES_H_SW_PATCH_VERSION))
#error "Opps, RegHelper.h and Platform_Types.h Software version not match!"
#endif
#endif  /* !DISABLE_MCAL_SW_VER_CHECK */
/* Version Check End */
/* Version and Check End */

#define writell(v, a)   (*(volatile uint64*)(a)) = (uint64) (v)
#define readll(a)       (*(volatile uint64*)(a))
#define readl(a)        (*(volatile uint32*)(a))
#define writel(v, a)    (*(volatile uint32*)(a)) = (uint32) (v)
#define readh(a)        (*(volatile uint16*)(a))
#define writeh(v, a)    (*(volatile uint16*)(a)) = (uint16) (v)
#define readb(a)        (*(volatile uint8*)(a))
#define writeb(v, a)    (*(volatile uint8*)(a))  = (uint8) (v)


#define REG64(addr) ((volatile uint64 *)(addr))
#define REG32(addr) ((volatile uint32 *)(addr))
#define REG16(addr) ((volatile uint16 *)(addr))
#define REG8(addr) ((volatile uint8 *)(addr))

#define RMWREG32(addr, startbit, width, val)                                    \
              do{                                                               \
                uint32 temp = *REG32(addr);                                     \
                temp &= ~((((uint32)1u<<(width)) - 1u) << (startbit));          \
                temp |= ((uint32)(val) << (startbit));                          \
                *REG32(addr) = temp;                                            \
              }while(0)

#define reg32(addr)     (*(volatile uint32 *)(addr))
#define reg16(addr)     (*(volatile uint16 *)(addr))
#define reg8(addr)      (*(volatile uint8 *)(addr))

#define rmw32           RMWREG32

#define REG_READ32(a)   readl((a))

#define REG_WRITE32(v, a)   writel((v), (a))

#define REG_RMW32   RMWREG32

#define BIT_1(x, bit) ((x) & (1UL << (bit)))
#define BIT(nr)       ((uint32)(((uint32)1u) << (nr)))
#define BIT_MASK(x) (((x) >= (sizeof(unsigned long) * 8U)) ? (0xFFFFFFFFU) : ((1UL << (x)) - 1UL))

#define writel_rbc(v, a,msk)  writel((v),(a))

#define  RMWREG32_RBC(a,s,w,v,msk)   RMWREG32((a),(s),(w),(v))

#ifdef __cplusplus
}
#endif
#endif  /* REGHELPER_H */
